package com.yang.utils;

import com.yang.config.NoteListConfig;
import com.yang.entity.EmailTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.logging.SimpleFormatter;

/**
 * @author yangdacheng
 * @title: UtilsFactory 编号生成工程
 * @projectName note_api
 * @description: TODO
 * @date 2022/3/3 10:28 上午
 */
@Component
public class UtilsFactory {

    @Resource
    private NoteListConfig noteListConfig;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SendEmailUtils sendEmailUtils;

    //生成编号
    public String buildID(){
        return UUID.randomUUID().toString().replace("-","");
    }

    //生成验证码
    public String buildCode(){
        return String.valueOf(Math.round(Math.random() * (9999-1000) +1000));
    }

    //生成账号
    public String userNameBuild(Integer maxNumber,Integer minNumber){
        return String.valueOf(new Random()
                .nextInt(maxNumber)%(maxNumber-minNumber+1) + minNumber);
    }

    //普通时间格式化
    public Date dateFormat(String time,String simplePattern){
        SimpleDateFormat dateFormat = new SimpleDateFormat(simplePattern);
        Date date = new Date();
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //UTC时间格式化
    public Date interceptTime(String timeStr) {
        SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        timeStr = timeStr.replace("Z", " UTC"); //注意UTC前有空格
        try {
            Date date = utcFormat.parse(timeStr);
            String format = sdf.format(date);
            return dateFormat(format,"yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //时间格式化未字符串
    public String dateFormatStr(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    //验证码存储与发送
    public void reserveSendCode(EmailTemplate emailTemplate,String userEmail){
        String buildCode = buildCode(); //生成验证码
        emailTemplate.setCodeTemplate(emailTemplate.getCodeTemplate().
                replace("{1}", noteListConfig.getAppTitle()).replace("{2}", buildCode));
        redisTemplate.opsForValue().set(userEmail,buildCode,60 * 15, TimeUnit.MINUTES); //放到缓存中
        sendEmailUtils.sendCode(userEmail, emailTemplate.getCodeTemplate(), noteListConfig.getAppTitle()+"验证码");
    }
}
